package util;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;


public class Helper {
	private static LinkedHashMap<String, String[]> devFlds;
	private static LinkedHashMap<String, String[]> vehFlds;
	private static LinkedHashMap<String, String[]> fltFlds;
	private static LinkedHashMap<String, String[]> psnFlds;
	private static LinkedHashMap<String, String[]> hisDataFlds;
	private static Map<String, LinkedHashMap<String, String[]>> disFldsMap;

	static {
		devFlds = new LinkedHashMap<String, String[]>();
		// devFlds.put("属性", new String[] { "显示名称", "数值单位", "分隔符" });
		devFlds.put("depnm", new String[] { "部门名称：", "", "<br/>" });
		devFlds.put("snm", new String[] { "员工姓名：", "", ", " });
		devFlds.put("snb", new String[] { "编号：", "", "<br/>" });
		devFlds.put("rid", new String[] { "设备号：", "", ", " });
		devFlds.put("rtym", new String[] { "类型：", "", "<br/>" });
		devFlds.put("spd", new String[] { "速度：", " km/h", "<br/>" });
		devFlds.put("lstu", new String[] { "时间：", "", "<br/>" });
		// devFlds.put("position", new String[] { "位置：", "", "" });

		vehFlds = new LinkedHashMap<String, String[]>();
		vehFlds.put("depnm", new String[] { "部门名称：", "", "<br/>" });
		vehFlds.put("snm", new String[] { "员工姓名：", "", ", " });
		vehFlds.put("snb", new String[] { "编号：", "", "<br/>" });
		vehFlds.put("rid", new String[] { "车号：", "", ", " });
		vehFlds.put("rtym", new String[] { "类型：", "", "<br/>" });
		vehFlds.put("vnb", new String[] { "车牌号：", "", "<br/>" });
		vehFlds.put("spd", new String[] { "速度：", " km/h", ", " });
		vehFlds.put("angle", new String[] { "方向：", "°", "<br/>" });
		vehFlds.put("lstu", new String[] { "时间：", "", "<br/>" });
		vehFlds.put("dataValues", new String[] { "字段：", "", "<br/>" });
		
		// vehFlds.put("position", new String[] { "位置：", "", "" });

		fltFlds = new LinkedHashMap<String, String[]>();
		fltFlds.put("flno", new String[] { "航班号：", "", "<br/>" });
		fltFlds.put("regn", new String[] { "飞机号：", "", "<br/>" });
		fltFlds.put("actn", new String[] { "机型：", "", "<br/>" });
		fltFlds.put("adidFlag", new String[] { "进离港：", "", "<br/>" });
		fltFlds.put("vialc", new String[] { "飞机航线：", "", "<br/>" });
		fltFlds.put("sto", new String[] { "计划时间：", "", "<br/>" });
		fltFlds.put("eto", new String[] { "变更时间：", "", "<br/>" });
		fltFlds.put("ato", new String[] { "实际时间：", "", "" });

		psnFlds = new LinkedHashMap<String, String[]>();
		psnFlds.put("pname", new String[] { "名称：", "", "" });

		hisDataFlds = new LinkedHashMap<String, String[]>();
		hisDataFlds.put("__gps", new String[] { "dt", "lon", "lat" });
		hisDataFlds.put("depnm", new String[] { "部门名称：", "", "<br/>" });
		hisDataFlds.put("unm", new String[] { "员工姓名：", "", "<br/>" });
		hisDataFlds.put("rtym", new String[] { "设备类型：", "", "<br/>" });
		hisDataFlds.put("arnm", new String[] { "区位名称：", "", "<br/>" });
		hisDataFlds.put("spd", new String[] { "速度：", " km/h", ", " });
		hisDataFlds.put("drt", new String[] { "方向：", "°", "<br/>" });
		hisDataFlds.put("dt", new String[] { "时间：", "", "<br/>" });
		hisDataFlds.put("pos", new String[] { "位置：", "", "" });

		disFldsMap = new HashMap<String, LinkedHashMap<String, String[]>>();
		disFldsMap.put("Device", devFlds);
		disFldsMap.put("Vehicle", vehFlds);
		disFldsMap.put("Flight", fltFlds);
		disFldsMap.put("FltPlace", psnFlds);
		disFldsMap.put("TrackPoint", hisDataFlds);
	}
	

	public static String getObjectJSON(Object object) {
		return getObjectJSON(object, null);
	}

	public static String getObjectJSON(Object object, JsonPropFilter filter) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setJsonPropertyFilter(filter);

		if (JSONUtils.isArray(object)) {
			return JSONArray.fromObject(object, jsonConfig).toString();
		} else {
			return JSONObject.fromObject(object, jsonConfig).toString();
		}
	}

	public static String getMD5String(String source) {
		try {
			StringBuilder sb = new StringBuilder();
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(source.getBytes());
			byte[] digest = md.digest();

			for (int i = 0; i < digest.length; i++) {
				if ((digest[i] & 0xff) < 0x10) {
					sb.append("0");
				}

				sb.append(Long.toString(digest[i] & 0xff, 16));
			}

			return sb.toString();
		} catch (Exception e) {
			return null;
		}
	}

	public static String getDisFldsMap() {
		return getObjectJSON(disFldsMap);
	}
}
